﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KenLib.BusinessManager;
using DataInfo;

public partial class JOINT_frmJ_StaffLangAbilityInput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("語学力情報登録");
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("input");
        if (!IsPostBack)
        {
           
            this.get語学名();
            this.get検定種別コード();
        }
        if (Session["ModeUpdate"] != null)// Session Update se lay mode Update tu trang frmJ_EmpbasicInfo cua link 語学力情報
        {
            this.CheckUpdate();
        }
        else
        {
            if (Session["IdEmp"] == null || Session["IdEmp"] == "")
            {
                Response.Redirect("frmJ_MCompManager.aspx");
            }
        }
        ddl語学名.Focus();
        validate_ddl語学名.ErrorMessage =  GetError("ER00001");
        validate_txtレベル.ErrorMessage = GetError("ER00001");
        validate_ddl検定種別.ErrorMessage = GetError("ER00001");
        validate_txt検定受験日.ErrorMessage =GetError("ER00001");
        //validate_regular_txt検定受験日.ErrorMessage = "検定受験日" + GetError("ER00002");
        CustomValidator2.ErrorMessage =  GetError("ER00002");
        validateday_Custom.ErrorMessage =  GetError("ER00005");
        validate_txtスコスListening_Custom.ErrorMessage =  GetError("ER00002");
        validate_txtスコスwriting_Custom.ErrorMessage =  GetError("ER00002");
        validate_txtスコスreading.ErrorMessage =  GetError("ER00002");
        CustomValidator1.ErrorMessage =  GetError("ER00009");

       
    }
    protected void btn_戻る_Click(object sender, EventArgs e)
    {
        if (Session["ModeUpdate"] != null)
        {
            Response.Redirect("frmJ_EmpBasicInfo.aspx");
        }
        else
        {
            Response.Redirect("frmJ_StaffCommutingInput.aspx");
        }
    }
    protected void btn_次_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_StaffDiplomaInput.aspx");
    }
    public void get語学名()
    {
        try
        {
            DataTable dt = new DataTable();
            語学コードManager frm = new 語学コードManager();
            dt = frm.Select_DDLTraineeLangAbilityInput();
            if (dt.Rows.Count > 0)
            {
                ddl語学名.DataSource = dt;
                ddl語学名.DataTextField = "語学名";
                ddl語学名.DataValueField = "ID";
                ddl語学名.DataBind();
                ListItem item = new ListItem("[選択]", "-1");
                item.Selected = true;
                ddl語学名.Items.Insert(0, item);
                
            }
        }
        catch { }
    }
    public void get検定種別コード()
    {
        try
        {
            DataTable dt = new DataTable();
            検定種別コードManager frm = new 検定種別コードManager();
            dt = frm.Select_DDL検定種別コードTraineeLangAbilityInput();
            if (dt.Rows.Count > 0)
            {
                ddl検定種別.DataSource = dt;
                ddl検定種別.DataTextField = "検定種別";
                ddl検定種別.DataValueField = "検定種別コード";
                ddl検定種別.DataBind();
                ListItem item = new ListItem("[選択]", "-1");
                item.Selected = true;
                ddl検定種別.Items.Insert(0, item);

            }
        }
        catch { }
    }
    public void CheckUpdate()
    {
        try
        {
            int ID = Int32.Parse(Session["Id"].ToString());
            btn_次.Enabled = false;
            DataTable dt = new DataTable();
            語学力情報Manager frm = new 語学力情報Manager();
            dt = frm.Select_frmJ_StaffLangAbilityInput(ID);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    ddl語学名.SelectedValue = row["語学コード"].ToString();
                    txtレベル.Text = row["レベル"].ToString();
                    ddl検定種別.SelectedValue = row["検定種別コード"].ToString();
                    txt検定受験日.Text = row["検定受験日"].ToString();
                    txtスコスListening.Text = row["スコスListening"].ToString();
                    txtスコスwriting.Text = row["スコスWriting"].ToString();
                    txtスコスreading.Text = row["スコスReading"].ToString();

                }
            }
        }
        catch { }
    }


    protected void btn_登録_Click(object sender, EventArgs e)
    {
        try
        {

            if (Session["ModeUpdate"] != null)// Session Update se lay mode Update tu trang frmJ_EmpbasicInfo cua link 語学力情報
            {
                語学力情報Manager frm = new 語学力情報Manager();
                語学力情報 obj = new 語学力情報();
                obj.ID = 27;// Session ID se lay ID tu trang frmJ_EmpbasicInfo cua link 語学力情報 
                obj.語学コード1 = Int32.Parse(ddl語学名.SelectedValue.ToString());
                obj.語学名 = ddl語学名.SelectedItem.Text.ToString();
                obj.レベル = txtレベル.Text.ToString();
                obj.検定種別コード1 = Int32.Parse(ddl検定種別.SelectedValue.ToString());
                obj.検定種別 = ddl検定種別.SelectedItem.Text.ToString();
                obj.検定受験日 = DateTime.Parse(txt検定受験日.Text.ToString());
                string スコスListening = txtスコスListening.Text.ToString();
                int Num;
                bool isNum = int.TryParse(スコスListening, out Num);

                if (isNum)
                {
                    obj.スコスListening = Int32.Parse(txtスコスListening.Text.ToString());
                }

                string スコスwriting = txtスコスwriting.Text.ToString();
                int Num1;
                bool isNum1 = int.TryParse(スコスwriting, out Num1);

                if (isNum1)
                {
                    obj.スコスWriting = Int32.Parse(txtスコスwriting.Text.ToString());
                }

                string スコスreading = txtスコスreading.Text.ToString();
                int Num2;
                bool isNum2 = int.TryParse(スコスreading, out Num2);
                if (isNum2)
                {
                    obj.スコスReading = Int32.Parse(txtスコスreading.Text.ToString());
                }
                frm.Update_frmJ_StaffLangAbilityInput(obj);
                Response.Redirect("frmJ_EmpBasicInfo.aspx");

            }

            else
            {
                if (CustomValidator1.IsValid == true)
                {
                    語学力情報Manager frm = new 語学力情報Manager();
                    語学力情報 obj = new 語学力情報();
                    obj.廃止区分 = 0;
                    obj.語学コード1 = Int32.Parse(ddl語学名.SelectedValue.ToString());
                    obj.語学名 = ddl語学名.SelectedItem.Text.ToString();
                    obj.レベル = txtレベル.Text.ToString();
                    obj.検定種別コード1 = Int32.Parse(ddl検定種別.SelectedValue.ToString());
                    obj.検定種別 = ddl検定種別.SelectedItem.Text.ToString();
                    obj.検定受験日 = DateTime.Parse(txt検定受験日.Text.ToString());

                    string スコスListening = txtスコスListening.Text.ToString();
                    int Num;
                    bool isNum = int.TryParse(スコスListening, out Num);

                    if (isNum)
                    {
                        obj.スコスListening = Int32.Parse(txtスコスListening.Text.ToString());
                    }

                    string スコスwriting = txtスコスwriting.Text.ToString();
                    int Num1;
                    bool isNum1 = int.TryParse(スコスwriting, out Num1);

                    if (isNum1)
                    {
                        obj.スコスWriting = Int32.Parse(txtスコスwriting.Text.ToString());
                    }

                    string スコスreading = txtスコスreading.Text.ToString();
                    int Num2;
                    bool isNum2 = int.TryParse(スコスreading, out Num2);
                    if (isNum2)
                    {
                        obj.スコスReading = Int32.Parse(txtスコスreading.Text.ToString());
                    }
                    obj.社員ID = Int32.Parse(Session["IdEmp"].ToString()); // ID lay tu sesssion khi dang ky tu trang frmJ_StaffBasicInfoInput
                    frm.Insert_frmJ_StaffLangAbilityInput(obj);
                    Response.Redirect("frmJ_StaffDiplomaInput.aspx");
                }
            }
        }
        catch { }
    }

    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
        {
            return error = dt.Rows[0][2].ToString();
        }
        else
        {
            return error;
        }


    }


    protected void CustomValidate語学名(object source, ServerValidateEventArgs args)
    {
        try
        {

            if (Session["IdEmp"] != null)
            {

                語学力情報Manager frm = new 語学力情報Manager();
                DataTable dt = new DataTable();
                dt = frm.Select_frmJ_StaffLangAbilityInput1();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        string 語学コード = ddl語学名.SelectedValue.ToString();
                        string 社員ID = Session["IdEmp"].ToString();// ID lay tu sesssion khi dang ky tu trang frmJ_StaffBasicInfoInput
                        string 検定受検日 = txt検定受験日.Text.ToString();
                        string date = row["検定受験日"].ToString();
                        DateTime date1 = DateTime.Parse(date);
                        date = date1.ToString("yyyy/MM/dd");
                        if (row["語学コード"].ToString() == 語学コード && date == 検定受検日 && row["社員ID"].ToString() == 社員ID)
                        {
                            args.IsValid = false;
                            return;
                        }
                        else
                        {
                            args.IsValid = true;
                        }
                    }

                }
            }
        }
        catch { }
    }


    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        if (Session["ModeUpdate"] != null)
        {
            this.CheckUpdate();
        }
        else
        {
            Response.Redirect("frmJ_StaffLangAbilityInput.aspx");
        }
    }
}
